package com.example.base.tools

import android.graphics.Bitmap
import android.widget.ImageView
import com.bumptech.glide.request.target.BitmapImageViewTarget
import com.bumptech.glide.request.transition.Transition

class MsgImageTarget(view: ImageView?) : BitmapImageViewTarget(view) {
    // 长图，宽图比例阈值
    companion object {
        const val RATIO_OF_LARGE = 2
        const val HW_RATIO = 2


    }

    override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
        super.onResourceReady(resolveBitmap(resource), transition)
    }

    private fun resolveBitmap(resource: Bitmap): Bitmap {

        val srcWidth = resource.width
        val srcHeight = resource.height
        if (srcWidth > srcHeight) {
            val srcWHRatio = srcWidth / srcHeight
// 宽图
            if (srcWHRatio > RATIO_OF_LARGE) {
                return Bitmap.createBitmap(resource, 0, 0, srcHeight * HW_RATIO, srcHeight)
            }
        } else {
            val srcHWRatio = srcHeight / srcWidth
            //长图
            if (srcHWRatio > RATIO_OF_LARGE) {
                return Bitmap.createBitmap(resource, 0, 0, srcWidth, srcWidth * HW_RATIO)
            }
        }
        return resource
    }

}